package team.knowers.qzhserver.course.datum.file.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.knowers.qzhserver.common.api.CommonResult;
import team.knowers.qzhserver.course.datum.datum.entity.dao.DatumDO;
import team.knowers.qzhserver.course.datum.datum.service.DatumService;
import team.knowers.qzhserver.course.datum.file.entity.dao.FileDO;
import team.knowers.qzhserver.course.datum.file.entity.dto.FileUploadDTO;
import team.knowers.qzhserver.course.datum.file.service.FileService;
import team.knowers.qzhserver.user.user.service.UserService;

import java.util.Objects;

/**
 * @author zcb
 * @date 2024/3/9 21:30
 */
@Controller
@Tag(name = "FileController",description = "资料文件操作")
@RequestMapping("/file")
public class FileController {
    @Autowired
    FileService fileService;
    @Autowired
    DatumService datumService;
    @Autowired
    UserService userService;

    @Operation(summary = "上传一个新文件信息")
    @PostMapping("/upload")
    @ResponseBody
    public CommonResult uploadFile(@RequestBody FileUploadDTO fileUploadDTO){
        //防止参数不合理
        DatumDO datumDO = datumService.getDatumById(fileUploadDTO.getCdid());
        if (datumDO==null){
            return CommonResult.failed("资料库不存在");
        }
        if (userService.userStatus(fileUploadDTO.getUid())==-1){
            return CommonResult.failed("用户不存在");
        }
        if(userService.userStatus(fileUploadDTO.getUid())==0){
            return CommonResult.failed("用户被封禁");
        }
        if(datumDO.getStatus()==2){
            return CommonResult.failed("资料库被锁定");
        }
        if(datumDO.getStatus()==1 && !Objects.equals(datumDO.getUserId(), fileUploadDTO.getUid())){
            return CommonResult.failed("资料库为私有，且操作者不是资料库拥有者");
        }
        FileDO fileDO = fileService.uploadFile(fileUploadDTO);
        if(fileDO!=null){
            return CommonResult.success(fileDO,"资料上传成功");
        }else
            return CommonResult.failed("资料上传失败");
    }
    @Operation(summary = "删除一个资料")
    @DeleteMapping("/delete")
    @ResponseBody
    public CommonResult deleteFile(@RequestParam String id,@RequestParam String uid){
        if (userService.userStatus(uid)==-1){
            return CommonResult.failed("用户不存在");
        }
        FileDO fileDO = fileService.getFileById(id);
        if(fileDO==null){
            return CommonResult.failed("资料不存在");
        }
        if(!Objects.equals(fileDO.getUserId(), uid) && !Objects.equals(datumService.getDatumById(fileDO.getCourseDatumId()).getUserId(), uid)){
            return CommonResult.failed("操作者不是文件上传者或资料库拥有者");
        }
        if(fileService.deleteFile(id)){
            return CommonResult.success(null,"删除资料成功");
        }else {
            return CommonResult.failed("删除资料失败");
        }
    }
}
